<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="../../resources/js-test.js"/>
</head>
<body>

<div id="console"/>

<div id="foo"/>
<div id="bar"/>

<svg id="svg-root" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <g id="test-body-content">
    <defs>
      <rect id="rect"/>
    </defs>
    <svg id="svgTree1">
      <rect id="subElem1"/>
    </svg>
    <svg>
      <rect id="subElem2"/>
    </svg>
    <svg id="svgTree3">
      <use xlink:href="#rect"/>
    </svg>
    <svg id="svgTree4">
      <rect id="foo"/>
    </svg>
    <svg id="svgTree5">
      <rect id="bar"/>
      <rect id="bar"/>
    </svg>
    <rect id="siblingElem1"/>
    <foreignObject id="foreign">
      <div id="nonsvg"/>
    </foreignObject>
  </g>
</svg>

<div id="foo"/>

<script><![CDATA[
  if (window.testRunner)
    testRunner.dumpAsText();
  var svgTree1 = document.getElementById("svgTree1");

  // Test support for "getElementById" on svgTree1
  var subElem1 = svgTree1.getElementById("subElem1");
  shouldBeTrue("null != subElem1");
  shouldBe(subElem1.id, "subElem1");

  // Test that "getElementById" on svgTree1 can't access children in other subtrees
  shouldBeTrue("null == svgTree1.getElementById('subElem2')");

  // Test that "getElementById" on svgTree1 can't access elements that are not its children
  shouldBeTrue("null == svgTree1.getElementById('siblingElem1')");

  // Test that an element outside of the subtree referenced through <use> is not found
  var svgTree3 = document.getElementById("svgTree3");
  shouldBeTrue("null == svgTree3.getElementById('rect')");

  // Test that the <svg> being searched on is not found itself
  shouldBeTrue("null == svgTree3.getElementById('svgTree3')");

  // Test that search in svg returns the id from the subtree, not the div with the same id
  var root = document.getElementById("svg-root");
  var svgTree4 = document.getElementById("svgTree4");
  shouldBeTrue("svgTree4.firstChild.nextSibling == root.getElementById('foo')");

  // Test that search in svg subtree returns the first of the duplicates, not the div with the same id
  var svgTree5 = document.getElementById("svgTree5");
  var bar = root.getElementById("bar");
  shouldBeTrue("svgTree5.firstChild.nextSibling == bar");

  // Test that search in svg subtree returns the first of the duplicates
  shouldBeTrue("svgTree5.firstChild.nextSibling == svgTree5.getElementById('bar')");

  // Test that non SVG content can be found in the svg subtree
  var nonsvg = root.getElementById("nonsvg");
  var foreign = root.getElementById("foreign");
  shouldBeTrue("foreign.firstChild.nextSibling == nonsvg");

  // Test that SVG element can find child elements when svg is not part of document yet
  var orphanSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  var childSvg = document.createElementNS('http://www.w3.org/2000/svg', 'g');
  orphanSvg.appendChild(childSvg)
  childSvg.id = 'fooSvg2'
  shouldBeTrue("childSvg == orphanSvg.getElementById('fooSvg2')");
]]></script>

</body>
</html>
